package com.wslds.blog.action;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.BaseAction;
import com.wslds.base.util.Strings;

@SuppressWarnings("serial")
@Component("utilAction")
@Scope("prototype")
public class UtilAction extends BaseAction {
	
	private static final String suffix = "westlife";
	
	public String login(){
		
		String passwd = getHttpRequest().getParameter("passwd");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String prefix = sdf.format(date);
		
		if( (prefix+suffix).equals(passwd) ){
			log.info("登陆成功. ip:" + getHttpRequest().getRemoteAddr() );
			getHttpSession().setAttribute("meId", "OK");
			return SUCCESS;
		}else{
			log.info("登陆失败,密码错误. ip:" + getHttpRequest().getRemoteAddr() );
			return "relogin";
		}
	}
	
	public String reveicePass(){
		try {
			List<String> questionList = new ArrayList<String>();
			List<String> answerList = new ArrayList<String>();
			Map<String,String> questions = Strings.genRandomQuestion();
			Set<String> keys = questions.keySet();
			for( Iterator<String> i = keys.iterator(); i.hasNext(); ){
				String key = (String)i.next();
				questionList.add(key);
				answerList.add(questions.get(key));
			}
			writePlainText(questionList + "`" + answerList);
			return NONE;
		} catch (Exception e) {
			log.error(MessageFormat.format("获取验证码出错,异常:{0}",Strings.getTrace(e)));
			writePlainText("");
			return NONE;
		}
	}

}
